package com.plpm.model;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * @author RAN
 * @data 2025/3/21 16:00
 * @Description: 返回体
 */
@Setter
@Getter
public class Result<T> implements Serializable {
    /**
     * 成功状态码
     */
    public static final int SUCCESS_CODE = 200;
    /**
     * 默认失败状态码
     */
    public static final int ERROR_CODE = 500;
    /**
     * 状态码
     */
    private int code;
    /**
     * 返回消息
     */
    private String message;
    /**
     * 泛型数据
     */
    private T result;

    /**
     * 私有构造器
     */
    private Result() {
    }

    /**
     * 成功静态方法（无数据）
     */
    public static <T> Result<T> success() {
        return success();
    }

    /**
     * 成功静态方法（带数据）
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(SUCCESS_CODE);
        result.setResult(data);
        result.setMessage("success");
        return result;
    }

    /**
     * 失败静态方法
     */
    public static <T> Result<T> error(String message) {
        return error(ERROR_CODE, message);
    }

    /**
     * 自定义错误码的失败方法
     */
    public static <T> Result<T> error(int code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public Result<T> code(int code) {
        this.code = code;
        return this;
    }

    public Result<T> msg(String msg) {
        this.message = msg;
        return this;
    }

    public Result<T> data(T data) {
        this.result = data;
        return this;
    }

    // 判断是否成功
    public boolean isSuccess() {
        return this.code == SUCCESS_CODE;
    }

}
